home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / GLperf3.12-src.lha / GLperf / CopyPix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-01  |  10.7 KB  |  298 lines

  1. /*
  2.  * (c) Copyright 1995, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * Author: John Spitzer, SGI Applied Engineering
  36.  *
  37.  */
  38. #include <math.h>
  39. #include "CopyPix.h"
  40. #include "CopyPixX.h"
  41.  
  42. #undef offset
  43. #define offset(v) offsetof(CopyPixels,v)
  44.  
  45. static InfoItem CopyPixelsInfo[] = {
  46. #define INC_REASON INFO_ITEM_ARRAY
  47. #include "CopyPix.h"
  48. #undef INC_REASON
  49. };
  50. #include <malloc.h>
  51.  
  52. CopyPixelsPtr new_CopyPixels()
  53. {
  54.     CopyPixelsPtr this = (CopyPixelsPtr)malloc(sizeof(CopyPixels));
  55.  
  56.     ImagePtr thisImage = (ImagePtr)(&this->image_CopyPixels);
  57.     TransferMapPtr thisTransferMap = (TransferMapPtr)(&this->transfermap_CopyPixels);
  58.     ZoomPtr thisZoom = (ZoomPtr)( &this->zoom_CopyPixels);
  59.  
  60.     CheckMalloc(this);
  61.     new_RasterPos((RasterPosPtr)this);
  62.     new_Image(thisImage);
  63.     new_TransferMap(thisTransferMap);
  64.     new_Zoom(thisZoom);
  65.     SetDefaults((TestPtr)this, CopyPixelsInfo);
  66.     this->testType = CopyPixelsTest;
  67.     this->numObjects = 1;
  68.     this->clearBefore = False;
  69.     this->traversalData = 0;
  70.     this->subImageData = 0;
  71.     this->usecPixelPrint = " microseconds per pixel with CopyPixels";
  72.     this->ratePixelPrint = " pixels per second with CopyPixels";
  73.     this->usecPrint = " microseconds per image with CopyPixels";
  74.     this->ratePrint = " CopyPixel images per second";
  75.     /* Set virtual functions */
  76.     this->SetState = CopyPixels__SetState;
  77.     this->delete = delete_CopyPixels;
  78.     this->Copy = CopyPixels__Copy;
  79.     this->Initialize = CopyPixels__Initialize;
  80.     this->Cleanup = CopyPixels__Cleanup;
  81.     this->SetExecuteFunc = CopyPixels__SetExecuteFunc;
  82.     this->PixelSize = CopyPixels__Size;
  83.     this->TimesRun = CopyPixels__TimesRun;
  84.     return this;
  85. }
  86.  
  87. void delete_CopyPixels(TestPtr thisTest)
  88. {
  89.     CopyPixelsPtr this = (CopyPixelsPtr)thisTest;
  90.  
  91.     ImagePtr thisImage = (ImagePtr)(&this->image_CopyPixels);
  92.     TransferMapPtr thisTransferMap = (TransferMapPtr)(&this->transfermap_CopyPixels);
  93.     ZoomPtr thisZoom = (ZoomPtr)( &this->zoom_CopyPixels);
  94.  
  95.     delete_Zoom(thisZoom);
  96.     delete_TransferMap(thisTransferMap);
  97.     delete_Image(thisImage);
  98.     delete_RasterPos(thisTest);
  99. }
  100.  
  101. TestPtr CopyPixels__Copy(TestPtr thisTest)
  102. {
  103.     CopyPixelsPtr this = (CopyPixelsPtr)thisTest;
  104.     CopyPixelsPtr newCopyPixels = new_CopyPixels();
  105.     FreeStrings((TestPtr)newCopyPixels);
  106.     *newCopyPixels = *this;
  107.     CopyStrings((TestPtr)newCopyPixels, (TestPtr)this);
  108.     return (TestPtr)newCopyPixels;
  109. }
  110.  
  111. void CopyPixels__Initialize(TestPtr thisTest)
  112. {
  113.     CopyPixelsPtr this = (CopyPixelsPtr)thisTest;
  114.     int windowDim = min(this->environ.windowWidth, this->environ.windowHeight);
  115.     int windowArea = windowDim * windowDim;
  116.     GLint *intTraversalData;
  117.     GLint *srcPtr;
  118.     GLint *srcimageData;
  119.     GLfloat *dstPtr;
  120.     int total, index, offset;
  121.     int i;
  122.     float xZoomSgn = (this->zoom_CopyPixels.pixelZoomX >= 0.) ? 1. : -1.;
  123.     float yZoomSgn = (this->zoom_CopyPixels.pixelZoomY >= 0.) ? 1. : -1.;
  124.     float xFudge = .375;
  125.     float yFudge = .375;
  126.  
  127.     /* Layout RasterPos coordinates */
  128.     this->numDrawn = 0;
  129.     intTraversalData = CreateSubImageData(windowDim, windowDim, 
  130.                           (float)this->copyPixelsWidth*fabs(this->zoom_CopyPixels.pixelZoomX),
  131.                           (float)this->copyPixelsHeight*fabs(this->zoom_CopyPixels.pixelZoomY),
  132.                           this->acceptObjs, this->rejectObjs, this->clipObjs,
  133.                           this->clipMode, this->clipAmount, this->drawOrder==Spaced, 
  134.                           this->memAlignment, &this->numDrawn);
  135.  
  136.     /* Convert RasterPos coordinates to NDC */
  137.     this->traversalData = (GLfloat*)AlignMalloc(sizeof(GLfloat) * this->rasterPosDim * this->numDrawn, this->memAlignment);
  138.  
  139.     srcPtr = intTraversalData;
  140.     dstPtr = this->traversalData;
  141.     if (this->zOrder != Coplanar && this->rasterPosDim == 3) {
  142.     GLdouble modelMatrix[16];
  143.     GLdouble projMatrix[16];
  144.     GLint viewport[4];
  145.     GLdouble xd, yd, zd;
  146.     GLdouble depthBits, epsilon;
  147.     GLdouble base, range, delta;
  148.  
  149.         glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
  150.         glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
  151.         glGetIntegerv(GL_VIEWPORT, viewport);
  152.         glGetDoublev(GL_DEPTH_BITS, &depthBits);
  153.     epsilon = pow(2.0, -depthBits);
  154.         mysrand(15000);
  155.  
  156.     switch (this->zOrder) {
  157.     case Random:
  158.         range = 1. - epsilon;
  159.         base = epsilon;
  160.         delta = 0.;
  161.         break;
  162.     case BackToFront:
  163.         range = (1. - epsilon) / (GLdouble)this->numDrawn;
  164.         base = 1. - range - epsilon;
  165.         delta = -range;
  166.         break;
  167.     case FrontToBack:
  168.         range = (1. - epsilon) / (GLdouble)this->numDrawn;
  169.         base = epsilon;
  170.         delta = range;
  171.         break;
  172.     }
  173.  
  174.     for (i = 0; i < this->numDrawn; i++) {
  175.         /* Perturb the Z value to shake things up */
  176.         GLdouble x = (GLdouble)*srcPtr++ + xFudge;
  177.         GLdouble y = (GLdouble)*srcPtr++ + yFudge;
  178.         GLdouble z = base + 
  179.                          delta * (GLdouble)i +
  180.                          range * (GLdouble)myrand()/(GLdouble)MY_RAND_MAX;
  181.         
  182.         gluUnProject(x, y, z,
  183.                          modelMatrix, projMatrix, viewport,
  184.                          &xd, &yd, &zd);
  185.             *dstPtr++ = (GLfloat)xd * xZoomSgn;
  186.             *dstPtr++ = (GLfloat)yd * yZoomSgn;
  187.         *dstPtr++ = (GLfloat)zd;
  188.     }
  189.     } else {
  190.     for (i = 0; i < this->numDrawn; i++) {
  191.             *dstPtr++ = (((double)(*srcPtr++) + xFudge) / (double)windowDim * 2. - 1.) *
  192.                         xZoomSgn;
  193.             *dstPtr++ = (((double)(*srcPtr++) + yFudge) / (double)windowDim * 2. - 1.) *
  194.                         yZoomSgn;
  195.         if (this->rasterPosDim == 3) *dstPtr++ = -1.;
  196.     }
  197.     }
  198.     AlignFree(intTraversalData);
  199.  
  200.     /* Layout offsets in window coordinates for source (from) data.
  201.      * All the from images should lie entirely within the source image, so 100% of 
  202.      * our offsets should be "accepted".  numDrawn taken from RasterPos layout above.
  203.      */
  204.     srcimageData = CreateSubImageData(windowDim, windowDim,
  205.                  this->copyPixelsWidth,
  206.                  this->copyPixelsHeight,
  207.                                  1., 0., 0., Random, 0., this->drawOrder==Spaced,
  208.                                  this->memAlignment, &this->numDrawn);
  209.  
  210.     /* Now rotate these around to make sure the src and dst aren't the same... */
  211.     this->subImageData = (GLint*)AlignMalloc(sizeof(GLint) * 2 * this->numDrawn, this->memAlignment);
  212.     offset = (this->numDrawn + 1) / 2;
  213.     total = this->numDrawn;
  214.     for (i = 0; i < total; i++) {
  215.     index = (i + offset) % (total-1);
  216.     this->subImageData[2 * index] = srcimageData[2 * i];
  217.     this->subImageData[2 * index + 1] = srcimageData[2 * i + 1];
  218.     }
  219.     AlignFree(srcimageData);
  220.  
  221.     /* Finally, draw something interesting before we start copying.  Otherwise, we won't
  222.      * see anything going on!  Remember that we set the clearBefore flag to False up
  223.      * in the creator.  This prevents the test executor from clearing buffers before testing.
  224.      */
  225.  
  226.   Image__DrawSomething(this->environ.bufConfig.rgba, this->environ.bufConfig.indexSize,
  227.                this->environ.bufConfig.doubleBuffer);
  228. }
  229.  
  230. void CopyPixels__Cleanup(TestPtr thisTest)
  231. {
  232.     CopyPixelsPtr this = (CopyPixelsPtr)thisTest;
  233.  
  234.     if (this->traversalData) AlignFree(this->traversalData);
  235.     if (this->subImageData) AlignFree(this->subImageData);
  236. }
  237.  
  238. int CopyPixels__SetState(TestPtr thisTest)
  239. {
  240.     CopyPixelsPtr this = (CopyPixelsPtr)thisTest;
  241.  
  242.     ImagePtr thisImage = (ImagePtr)(&this->image_CopyPixels);
  243.     TransferMapPtr thisTransferMap = (TransferMapPtr)(&this->transfermap_CopyPixels);
  244.     ZoomPtr thisZoom = (ZoomPtr)( &this->zoom_CopyPixels);
  245.  
  246.     /* set parent state */
  247.     if (RasterPos__SetState(thisTest) == -1) return -1;
  248.  
  249.     /* set other inherited classes' states */
  250.     if (Image__SetState(thisImage) == -1) return -1;
  251.     if (TransferMap__SetState(thisTransferMap) == -1) return -1;
  252.     if (Zoom__SetState(thisZoom) == -1) return -1;
  253.  
  254.     /* set own state */
  255.     glReadBuffer(this->readBuffer);
  256.  
  257.     return 0;
  258. }
  259.  
  260. void CopyPixels__SetExecuteFunc(TestPtr thisTest)
  261. {
  262.     CopyPixelsPtr this = (CopyPixelsPtr)thisTest;
  263.     CopyPixelsFunc function;
  264.  
  265.     function.word = 0;
  266.  
  267.     function.bits.functionPtrs = this->loopFuncPtrs;
  268.  
  269.     /* Dimensions of data to be traversed */
  270.     function.bits.rasterPosDim  = this->rasterPosDim - 2;
  271.  
  272.     this->Execute = CopyPixelsExecuteTable[function.word];
  273. }
  274.  
  275. int CopyPixels__TimesRun(TestPtr thisTest)
  276. {
  277.     CopyPixelsPtr this = (CopyPixelsPtr)thisTest;
  278.     return this->numDrawn;
  279. }
  280.  
  281. float CopyPixels__Size(TestPtr thisTest)
  282. {
  283.     CopyPixelsPtr this = (CopyPixelsPtr)thisTest;
  284.     int accept = this->acceptObjs * (float)(this->numDrawn);
  285.     int reject = this->rejectObjs * (float)(this->numDrawn);
  286.     int clip = this->clipObjs * (float)(this->numDrawn);
  287.     float size = (float)this->copyPixelsWidth * fabs(this->zoom_CopyPixels.pixelZoomX) * 
  288.                  (float)this->copyPixelsHeight * fabs(this->zoom_CopyPixels.pixelZoomY);
  289.     float acceptSize, clipSize;
  290.  
  291.     accept += this->numDrawn - accept - reject - clip;
  292.  
  293.     acceptSize = (float)accept * size;
  294.     clipSize = (float)clip * (1. - this->clipAmount) * size;
  295.  
  296.     return (acceptSize + clipSize)/(float)this->numDrawn;
  297. }
  298.